<template>
  <div style="padding: 16px 24px">
    <div class="info-tip">
      <div>{{ $t("机器人作为Master主站客户端时") }}</div>
      <div>{{ $t("可通过第三方Slave获取外部传感器数据") }}</div>
      <div>
        {{
          $t(
            "可通过第三方Slave对外部控制器下达指令如灯光风机控制器控制灯光风机开关等"
          )
        }}
      </div>
    </div>
    <div style="margin-bottom: 24px">
      <div class="info-box">
        <div class="info-titlt" style="margin: 24px 0; margin-right: 10px">
          {{ $t("第三方Slave列表") }}
        </div>
        <div
          style="
            height: 1px;
            flex: 1;
            border-top: 1px dashed rgba(219, 255, 251, 0.2);
          "
        ></div>
      </div>
      <div
        style="
          display: flex;
          margin-right: -16px;
          margin-bottom: -16px;
          flex-wrap: wrap;
        "
      >
        <div
          v-for="(item, index) in slaveList"
          :key="item.id"
          class="slaveBox"
          :style="{ border: activeIndex == index ? '1px solid #00D1FF' : '' }"
        >
          <div class="slaveTitle">
            <span class="slaveName">{{ item.plcName }}</span>
            <div style="display: inline-block; float: right">
              <img
                @click="slaveEdit(item)"
                class="slaveAction"
                style="margin-right: 10px"
                src="../../../../assets/images/system/robot-edit.png"
                alt=""
              />
              <img
                @click="slaveDel(item)"
                class="slaveAction"
                src="../../../../assets/images/system/robot-del.png"
                alt=""
              />
            </div>
          </div>
          <div class="slaveContent" @click="choiceItem(item, index)">
            <div style="margin-bottom: 8px">
              <span style="color: #a6acb1">IP：</span>
              <span style="color: #fff">{{ item.plcIp }}</span>
            </div>
            <div style="margin-bottom: 8px">
              <span style="color: #a6acb1">{{ $t("端口号") }}：</span>
              <span style="color: #fff">{{ item.plcPort }}</span>
            </div>
            <div>
              <span style="color: #a6acb1">{{ $t("寄存器地址") }}：</span>
              <span style="color: #fff">{{ item.registerAddr }}</span>
            </div>
          </div>
        </div>
        <div class="slaveAdd">
          <img
            @click="slaveAdd"
            style="cursor: pointer"
            src="../../../../assets/images/system/robot-more.png"
            alt=""
          />
        </div>
      </div>
    </div>
    <div>
      <div class="info-box">
        <div class="info-titlt" style="margin-right: 10px">
          {{ $t("点位列表") }}
        </div>
        <div
          style="
            height: 1px;
            flex: 1;
            border-top: 1px dashed rgba(219, 255, 251, 0.2);
          "
        ></div>
      </div>
      <div
        class="search-area"
        style="border-bottom: 1px dashed rgba(219, 255, 251, 0.2)"
      >
        <div class="form">
          <el-form
            :model="listQuery"
            ref="listQuery"
            size="small"
            :inline="true"
          >
            <el-form-item :label="$t('巡检对象名称') + '：'">
              <el-input
                v-model="listQuery.name"
                :placeholder="$t('请输入')"
                clearable
                size="medium"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-form>
        </div>
        <div class="search-area-btn btns">
          <div @click="serachBtn" class="serch-btn cuver-point">
            {{ $t("搜索") }}
          </div>
          <div @click="resetFormBtn('listQuery')" class="reset-btn cuver-point">
            {{ $t("重置") }}
          </div>
        </div>
      </div>
      <div class="table" style="margin-top: 20px">
        <div class="self">
          <div class="btns">
            <div @click="addPoint" class="serch-btn cuver-point">
              {{ $t("新增") }}
            </div>
          </div>
          <el-table
            ref="tableRef"
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%; margin-top: 16px"
          >
            <el-table-column
              align="center"
              prop="name"
              :label="$t('巡检对象名称')"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="createTime"
              :label="$t('时间')"
            >
            </el-table-column>
            <el-table-column
              :label="$t('操作')"
              align="center"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <div class="tb-btn">
                  <div
                    @click="handleEdit(scope.row)"
                    class="tb-edit-btn cursor-pointer"
                  >
                    {{ $t("编辑") }}
                  </div>
                  <div
                    class="tb-del-btn cursor-pointer"
                    @click="handleDelete(scope.row)"
                  >
                    {{ $t("删除") }}
                  </div>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagenation">
          <pagination
            :total="total"
            :page.sync="listQuery.pageNum"
            :limit.sync="listQuery.pageSize"
            @pagination="getPlcDevicelist"
          >
          </pagination>
        </div>
      </div>
    </div>
    <el-drawer
      :title="$t('添加从站')"
      :visible.sync="slaveVisible"
      :before-close="slaveClose"
    >
      <el-form
        :model="slaveForm"
        ref="drawerRef"
        :rules="slaveFormRules"
        :inline="true"
        label-width="151px"
      >
        <el-form-item :label="$t('名称') + '：'" prop="plcName">
          <el-input
            style="width: 360px"
            v-model="slaveForm.plcName"
            clearable
          />
        </el-form-item>
        <el-form-item :label="$t('IP地址') + '：'" prop="plcIp">
          <el-input style="width: 360px" v-model="slaveForm.plcIp" clearable />
        </el-form-item>
        <el-form-item :label="$t('端口号') + '：'" prop="plcPort">
          <el-input-number
            controls-position="right"
            style="width: 360px"
            v-model="slaveForm.plcPort"
            clearable
          />
          <label style="color: #a6acb1">(1-65535)</label>
        </el-form-item>
        <el-form-item :label="$t('寄存器地址') + '：'" prop="registerAddr">
          <el-input-number
            controls-position="right"
            style="width: 360px"
            v-model="slaveForm.registerAddr"
            clearable
          />
        </el-form-item>
        <el-form-item :label="$t('数据位') + '：'" prop="dataBit">
          <el-input-number
            controls-position="right"
            style="width: 360px"
            v-model="slaveForm.dataBit"
            clearable
          />
        </el-form-item>
        <el-form-item :label="$t('HTTP地址') + '：'" prop="httpUrl">
          <el-input
            style="width: 360px"
            v-model="slaveForm.httpUrl"
            clearable
          />
        </el-form-item>
      </el-form>
      <div class="demo-drawer__footer drawer-footer">
        <div @click="submitSlave" class="serch-btn">{{ $t("确认") }}</div>
        <div @click="slaveVisible = false" class="reset-btn">
          {{ $t("取消") }}
        </div>
      </div>
    </el-drawer>
    <el-drawer
      :title="$t('添加点位')"
      :visible.sync="pointVisible"
      :before-close="pointClose"
    >
      <el-form
        :model="pointForm"
        ref="drawerRef"
        :rules="pointFormRules"
        :inline="true"
        label-width="165px"
      >
        <el-form-item :label="$t('巡检对象名称') + '：'" prop="name">
          <el-select filterable @change="changePoint" v-model="pointForm.name">
            <el-option
              v-for="item in pointData"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="demo-drawer__footer drawer-footer">
        <div @click="submitPoint" class="serch-btn">{{ $t("确认") }}</div>
        <div @click="pointVisible = false" class="reset-btn">
          {{ $t("取消") }}
        </div>
      </div>
    </el-drawer>
    <!-- slave删除弹框 -->
    <el-dialog
      :visible.sync="dialogVisible"
      :title="$t('删除确认')"
      width="600px"
      append-to-body
    >
      <div class="confirm-dialog">
        <div class="confirm-content">
          <div>
            <img
              src="../../../../assets/images/data/resource/mark.png"
              alt=""
            />
            <span>{{ $t("确认删除第三方Slave") + "：" }}</span>
            <span class="confirm-tip">{{ plcName }}?</span>
          </div>
          <div style="margin-top: 30px" class="confirm-tip">
            {{
              $t("注第三方Slave删除后配置的点位及自动生成的透传映射也随之删除")
            }}
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <div class="serch-btn" @click="plcDialogConfirm">
          {{ $t("确定删除") }}
        </div>
        <div class="reset-btn" style="margin-left: 15px" @click="closeDialog">
          {{ $t("取消") }}
        </div>
      </div>
    </el-dialog>
    <!-- 点位删除弹框 -->
    <el-dialog
      :visible.sync="pointDialog"
      :title="$t('删除确认')"
      width="600px"
      append-to-body
    >
      <div class="confirm-dialog">
        <div class="confirm-content">
          <div>
            <img
              src="../../../../assets/images/data/resource/mark.png"
              alt=""
            />
            <span>{{ $t("确认删除点位") + "：" }}</span>
            <span class="confirm-tip">{{ pointName }}?</span>
          </div>
          <div style="margin-top: 30px" class="confirm-tip">
            {{ $t("注点位删除后自动生成的透传也随之删除") }}
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <div class="serch-btn" @click="pointConfirm">
          {{ $t("确定删除") }}
        </div>
        <div
          class="reset-btn"
          style="margin-left: 15px"
          @click="pointCloseDialog"
        >
          {{ $t("取消") }}
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getPlcList,
  addPlc,
  editPlc,
  deletePlc,
  plcDevicelist,
  plcDeviceTree,
  plcDeviceAdd,
  plcDeviceEdit,
  plcDeviceDelete,
} from "@/api/system/network";

export default {
  data() {
    return {
      slaveList: [],
      listQuery: {
        name: "",
        plcId: "",
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      tableData: [],
      slaveVisible: false, //添加从站抽屉显隐
      slaveForm: {
        plcName: "",
        plcIp: "",
        plcPort: undefined,
        registerAddr: undefined,
        dataBit: undefined,
        httpUrl: "",
      },
      plcName: "", //带入删除弹框
      slaveFormRules: {
        plcName: [
          {
            required: true,
            message: this.$t("从站名称不能为空"),
            trigger: "blur",
          },
        ],
        plcIp: [
          {
            required: true,
            message: this.$t("IP地址不能为空"),
            trigger: "blur",
          },
        ],
        plcPort: [
          {
            required: true,
            message: this.$t("端口号不能为空"),
            trigger: "blur",
          },
        ],
        registerAddr: [
          {
            required: true,
            message: this.$t("寄存器地址不能为空"),
            trigger: "blur",
          },
        ],
        dataBit: [
          {
            required: true,
            message: this.$t("数据位不能为空"),
            trigger: "blur",
          },
        ],
      },
      pointForm: {
        name: "",
      },
      pointFormRules: {
        name: [
          {
            required: true,
            message: this.$t("巡检对象名称不能为空"),
            trigger: "blur",
          },
        ],
      },
      pointData: [],
      pointVisible: false,
      editId: "",
      dialogVisible: false,
      plcId: "", //用于删除plc
      deviceTreeId: "", //巡检对象id
      slaveId: "", //添加点位的slave id
      slaveIdDefault: "", //默认点位的slave id
      deviceEditId: "", //编辑点位的id
      pointDialog: false, //点位删除弹框
      pointName: "",
      pointDelId: "",
      activeIndex: 0,
    };
  },
  mounted() {
    this.getPlcList();
  },
  methods: {
    pointConfirm() {
      plcDeviceDelete({ id: this.pointDelId }).then((res) => {
        if (res.code == 200) {
          this.$message.success("删除成功");
          this.getPlcDevicelist();
        } else {
          this.$message.error("删除失败");
        }
        this.pointDialog = false;
      });
    },
    pointCloseDialog() {
      this.pointDialog = false;
    },
    //点击单个slave获取slaveId
    choiceItem(item, index) {
      this.activeIndex = index;
      this.slaveId = item.id;
      this.listQuery.plcId = item.id;
      this.getPlcDevicelist();
    },
    //获取slave列表
    getPlcList() {
      getPlcList().then((res) => {
        if (res.code == 200) {
          this.slaveList = res.data;
          this.slaveIdDefault = res.data[0].id; //记住默认id以便重置时默认第一个plc
          this.slaveId = res.data[0].id;
          this.listQuery.plcId = this.slaveIdDefault;
          this.getPlcDevicelist();
        } else {
          this.slaveList = [];
        }
      });
    },
    //获取点位列表
    getPlcDevicelist() {
      plcDevicelist(this.listQuery).then((res) => {
        if (res.code == 200) {
          this.tableData = res.rows;
          this.total = res.total;
        } else {
          this.tableData = [];
        }
      });
    },
    //获取巡检对象树列表
    getDeviceTree() {
      this.pointData = [];
      plcDeviceTree().then((res) => {
        if (res.code == 200) {
          if (res.data && res.data.length > 0) {
            res.data.forEach((item) => {
              this.pointData.push({
                value: item.treeId,
                label: item.name,
              });
            });
          }
        } else {
        }
      });
    },
    // 编辑slave列表
    slaveEdit(data) {
      this.slaveForm = {
        plcName: data.plcName,
        plcIp: data.plcIp,
        plcPort: data.plcPort,
        registerAddr: data.registerAddr,
        dataBit: data.dataBit,
        httpUrl: data.httpUrl,
        id: data.id,
      };
      this.editId = data.id;
      this.slaveVisible = true;
    },
    // 删除slave列表
    slaveDel(data) {
      this.dialogVisible = true;
      this.plcName = data.plcName;
      this.plcId = data.id;
    },
    //弹框 确认删除
    plcDialogConfirm() {
      deletePlc({ id: this.plcId }).then((res) => {
        if (res.code == 200) {
          this.$message.success("删除成功");
          this.dialogVisible = false;
          this.getPlcList();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    //弹框 取消删除
    closeDialog() {
      this.dialogVisible = false;
    },
    serachBtn() {
      this.listQuery.pageNum = 1;
      this.getPlcDevicelist();
    },
    resetFormBtn() {
      this.listQuery = {
        name: "",
        plcId: this.slaveIdDefault,
        pageNum: 1,
        pageSize: 10,
      };
      this.serachBtn();
    },
    slaveAdd() {
      this.slaveVisible = true;
      this.slaveForm = {
        plcName: "",
        plcIp: "",
        plcPort: undefined,
        registerAddr: undefined,
        dataBit: undefined,
        httpUrl: "",
      };
    },
    slaveClose() {
      this.slaveVisible = false;
    },
    submitSlave() {
      if (this.editId) {
        editPlc(this.slaveForm).then((res) => {
          if (res.code == 200) {
            this.$message.success(this.$t("修改成功"));
            this.slaveVisible = false;
            this.getPlcList();
          } else {
            this.slaveList = [];
          }
        });
      } else {
        addPlc(this.slaveForm).then((res) => {
          if (res.code == 200) {
            this.$message.success(this.$t("新增成功"));
            this.slaveVisible = false;
            this.getPlcList();
          } else {
            this.slaveList = [];
          }
        });
      }
    },
    addPoint() {
      this.pointVisible = true;
      this.deviceTreeId = "";
      this.pointForm.name = "";
      this.getDeviceTree();
    },
    pointClose() {
      this.pointVisible = false;
    },
    changePoint(id) {
      this.deviceTreeId = id;
    },
    submitPoint() {
      let params = {
        deviceTreeId: this.deviceTreeId,
        plcId: this.slaveId,
      };
      if (this.deviceEditId) {
        params.id = this.deviceEditId;
        plcDeviceEdit(params).then((res) => {
          if (res.code == 200) {
            this.$message.success(this.$t("修改成功"));
            this.pointVisible = false;
            this.getPlcDevicelist();
          } else {
            this.$message.error(this.$t("修改失败"));
          }
        });
      } else {
        plcDeviceAdd(params).then((res) => {
          if (res.code == 200) {
            this.$message.success(this.$t("新增成功"));
            this.pointVisible = false;
            this.getPlcDevicelist();
          } else {
            this.$message.error(this.$t("新增失败"));
          }
        });
      }
    },
    //编辑点位
    handleEdit(data) {
      this.deviceEditId = data.id;
      this.getDeviceTree();
      this.pointForm.name = data.name;
      this.deviceTreeId = data.deviceTreeId;
      this.pointVisible = true;
    },
    //删除点位
    handleDelete(data) {
      this.pointName = data.name;
      this.pointDelId = data.id;
      this.pointDialog = true;
    },
  },
};
</script>
<style lang="scss" scoped>
.slaveBox {
  width: 240px;
  border-radius: 2px;
  margin-right: 16px;
  margin-bottom: 16px;
  .slaveTitle {
    background-color: #33414c;
    height: 36px;
    line-height: 36px;
    padding: 0 16px;
    .slaveName {
      font-weight: bold;
      color: #ffffff;
      font-size: 14px;
    }
    .slaveAction {
      height: 20px;
      width: 20px;
      vertical-align: middle;
      cursor: pointer;
    }
  }
  .slaveContent {
    padding: 16px;
    font-size: 14px;
    background-color: #11202e;
    cursor: pointer;
  }
}
.slaveAdd {
  background-color: #11202e;
  width: 240px;
  height: 141px;
  position: relative;
  img {
    width: 40px;
    height: 40px;
    position: absolute;
    top: calc(50% - 20px);
    left: calc(50% - 20px);
  }
}
::v-deep .el-input {
  input {
    text-align: left;
  }
}
::v-deep .el-input-number__decrease {
  display: none;
}
::v-deep .el-input-number__increase {
  display: none;
}
</style>
